package com.woniu.new_orange.staff_module_dong.controller;

import com.woniu.new_orange.entity.Dept;
import com.woniu.new_orange.staff_module_dong.service.DeptService;
import com.woniu.new_orange.util.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Program: new_orange->DeptController
 * @Description: 部门controller
 * @Author: donghang
 * @Date: 2023-01-31 16:19
 * @Version： 1.0
 **/

@RestController
@RequestMapping("/depts")
@Api(value = "部门操作",tags = "部门详细操作")
public class DeptController {

    @Autowired
    private DeptService deptService;

    //  董航  2023/1/31 16:22  查询所有部门
    @GetMapping("")
    @ApiOperation(value = "查询现有所有部门",notes = "查询现有所有部门全部信息")
    public Result selAll(){
        List<Dept> depts = deptService.selAll();
        return Result.ok().data("deptList",depts);
    }

    //  董航  2023/1/31 16:24  新增部门
    @PostMapping("")
    @ApiOperation(value = "新增部门",notes = "新增部门")
    public Result add(@RequestBody Dept dept){
        int i = deptService.insertSelective(dept);
        return Result.ok().message("添加成功！");
    }
    //  董航  2023/1/31 16:24  移除部门
    @DeleteMapping("{deptId}")
    @ApiOperation(value = "移除部门",notes = "移除部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId",value = "部门id",required = true,dataType = "Long",paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 401,message = "未认证"),
            @ApiResponse(code = 403,message = "无权限"),
            @ApiResponse(code = 404,message = "未找到")})
    public Result del(@PathVariable Long deptId){
        int i = deptService.deleteByPrimaryKey(deptId);
        if(i == 8){
            return Result.error().message("部门中还有员工存存在，不能直接解散部门");
        } else {
            return Result.ok();
        }
    }
    //  董航  2023/1/31 16:25  修改部门信息
    @PutMapping("")
    @ApiOperation(value = "修改部门信息",notes = "修改部门信息")
    public Result update(@RequestBody Dept dept){
        int i = deptService.updateByPrimaryKeySelective(dept);
        return Result.ok().message("部门信息修改成功！");
    }


    //  董航  2023/1/31 16:25  查询当前部门信息
    @GetMapping("{deptId}")
    @ApiOperation(value = "查询当前部门信息",notes = "查询当前部门信息")
    public Result selOne(@PathVariable Long deptId){
        Dept dept = deptService.selectByPrimaryKey(deptId);
        return Result.ok().data("dept",dept);
    }
}
